package com.yeepay.yjpay.service.impl;

import java.util.TreeMap;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.yeepay.util.common.RandomUtil;
import com.yeepay.util.common.YJPayUtil;
import com.yeepay.util.encrypt.AES;
import com.yeepay.util.encrypt.EncryUtil;
import com.yeepay.util.encrypt.RSA;
import com.yeepay.yjpay.YeepayApiConfig;
import com.yeepay.yjpay.URIList.MerchantURIList;
import com.yeepay.yjpay.service.MerchantAPIService;

@Service("merchantAPIService")
public class MerchantAPIServiceImpl implements MerchantAPIService {

	// 商户自己随机生成的AESkey
	private String merchantAesKey = RandomUtil.getRandom(16);
	
	@Resource(name="yjpayutil")
	private YJPayUtil yjpayutil;
	
	@Resource(name="yeepayApiConfig")
	private YeepayApiConfig yeepayApiConfig;

	@Override
	public String driectRefund(String orderid, String origyborderid,
			String amount, String currency, String cause) throws Exception {

		String requestURL = this.yeepayApiConfig.getMerchantUrlprefix()
				+ MerchantURIList.DIRECT_REFUND_URI.getValue();

		TreeMap<String, Object> map = new TreeMap<String, Object>();
		map.put("cause", cause);
		map.put("amount", amount);
		map.put("currency", currency);
		map.put("merchantaccount", this.yeepayApiConfig.getMerchantAccount());
		map.put("orderid", orderid);
		map.put("origyborderid", origyborderid);

		// 生成RSA签名
		String sign = EncryUtil.handleRSA(map, this.yeepayApiConfig.getMerchantPrivatekey());

		// 商户自己随机生成的AESkey
		String merchantAesKey = RandomUtil.getRandom(16);

		map.put("sign", sign);

		// 生成data
		String info = JSON.toJSONString(map);
		String data = AES.encryptToBase64(info, merchantAesKey);

		// 使用RSA算法将商户自己随机生成的AESkey加密
		String encryptkey = RSA.encrypt(merchantAesKey, this.yeepayApiConfig.getYibaoPublickey());

		YJPayUtil yjpayutil = new YJPayUtil();

		// 请求一键支付接口
		String ybresult = yjpayutil.payAPIRequest(requestURL, data, encryptkey,
				this.yeepayApiConfig.getMerchantAccount(), true);

		// 将一键支付返回的结果进行验签，解密并返回
		return yjpayutil.checkYbResult(ybresult);
	}

	@Override
	public String queryRefund(String orderid, String yborderid)
			throws Exception {
		// 使用商户通用接口——退款查询
		String requestURL = this.yeepayApiConfig.getMerchantUrlprefix() + MerchantURIList.QUERY_REFUND.getValue();

		TreeMap<String, Object> map = new TreeMap<String, Object>();
		map.put("merchantaccount", this.yeepayApiConfig.getMerchantAccount());
		map.put("orderid", orderid);
		map.put("yborderid", yborderid);

		// 生成RSA签名
		String sign = EncryUtil.handleRSA(map, this.yeepayApiConfig.getMerchantPrivatekey());

		map.put("sign", sign);

		String info = JSON.toJSONString(map);
		String data = AES.encryptToBase64(info, merchantAesKey);

		// 使用RSA算法将商户自己随机生成的AESkey加密
		String encryptkey = RSA.encrypt(merchantAesKey, this.yeepayApiConfig.getYibaoPublickey());

		YJPayUtil yjpayutil = new YJPayUtil();

		// 请求一键支付接口
		String ybresult = yjpayutil.payAPIRequest(requestURL, data, encryptkey,
				this.yeepayApiConfig.getMerchantAccount(), false);

		// 将一键支付返回的结果进行验签，解密并返回
		return yjpayutil.checkYbResult(ybresult);
	}

	@Override
	public String queryOrder(String orderid, String yborderid) throws Exception {
		// 使用商户通用接口——交易查询
		String requestURL = this.yeepayApiConfig.getMerchantUrlprefix() + MerchantURIList.QUERY_PAY.getValue();

		TreeMap<String, Object> map = new TreeMap<String, Object>();
		map.put("merchantaccount", this.yeepayApiConfig.getMerchantAccount());
		if(orderid != null){
			map.put("orderid", orderid);
		}
		if(yborderid != null){
			map.put("yborderid", yborderid);

		}
			// 生成RSA签名
			String sign = EncryUtil.handleRSA(map, this.yeepayApiConfig.getMerchantPrivatekey());

			map.put("sign", sign);

			String info = JSON.toJSONString(map);
			String data = AES.encryptToBase64(info, merchantAesKey);

			// 使用RSA算法将商户自己随机生成的AESkey加密
			String encryptkey = RSA.encrypt(merchantAesKey, this.yeepayApiConfig.getYibaoPublickey());

			// 请求一键支付接口
			String ybresult = yjpayutil.payAPIRequest(requestURL, data, encryptkey,
					this.yeepayApiConfig.getMerchantAccount(), false);

			// 将一键支付返回的结果进行验签，解密并返回
			return this.yjpayutil.checkYbResult(ybresult);
		
	}

	@Override
	public String clearPayData(String startdate, String enddate)
			throws Exception {
		String requestURL = this.yeepayApiConfig.getMerchantUrlprefix()
				+ MerchantURIList.PAY_CLEAR_DATA.getValue();

		TreeMap<String, Object> map = new TreeMap<String, Object>();
		map.put("merchantaccount", this.yeepayApiConfig.getMerchantAccount());
		map.put("startdate", startdate);
		map.put("enddate", enddate);

		// 生成RSA签名
		String sign = EncryUtil.handleRSA(map, this.yeepayApiConfig.getMerchantPrivatekey());

		map.put("sign", sign);

		String info = JSON.toJSONString(map);
		String data = AES.encryptToBase64(info, merchantAesKey);

		// 使用RSA算法将商户自己随机生成的AESkey加密
		String encryptkey = RSA.encrypt(merchantAesKey, this.yeepayApiConfig.getYibaoPublickey());

		YJPayUtil yjpayutil = new YJPayUtil();

		// 请求一键支付接口
		String ybresult = yjpayutil.payAPIClearDataRequest(requestURL, data,
				encryptkey, this.yeepayApiConfig.getMerchantAccount(), false);

		// 将一键支付返回的结果进行验签，解密并返回
		return yjpayutil.checkYbClearResult(ybresult);
	}

	@Override
	public String clearRefundData(String startdate, String enddate)
			throws Exception {
		String requestURL = this.yeepayApiConfig.getMerchantUrlprefix()
				+ MerchantURIList.REFUND_CLEAR_DATA.getValue();

		TreeMap<String, Object> map = new TreeMap<String, Object>();
		map.put("merchantaccount", this.yeepayApiConfig.getMerchantAccount());
		map.put("startdate", startdate);
		map.put("enddate", enddate);

		// 生成RSA签名
		String sign = EncryUtil.handleRSA(map, this.yeepayApiConfig.getMerchantPrivatekey());

		map.put("sign", sign);

		String info = JSON.toJSONString(map);
		String data = AES.encryptToBase64(info, merchantAesKey);

		// 使用RSA算法将商户自己随机生成的AESkey加密
		String encryptkey = RSA.encrypt(merchantAesKey, this.yeepayApiConfig.getYibaoPublickey());

		YJPayUtil yjpayutil = new YJPayUtil();

		// 请求一键支付接口
		String ybresult = yjpayutil.payAPIClearDataRequest(requestURL, data,
				encryptkey, this.yeepayApiConfig.getMerchantAccount(), false);
		// 将一键支付返回的结果进行验签，解密并返回
		return yjpayutil.checkYbClearResult(ybresult);
	}

}
